﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace HM.DataAccessLayer.DTO
{
    /// <summary>
    /// Class that represent all the stats avaible for a for a player
    /// </summary>
    public class PlayerStats:INotifyPropertyChanged
    {

        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Id of the Player
        /// </summary>
        private Int32 _playerId;
        public Int32 PlayerId
        {
            get { return _playerId; }
            set
            {
                _playerId = value;
                Notify("PlayerId");
            }
        }


        private int _nbCashHands;
        /// <summary>
        /// Number of hands played in cash games for the periods
        /// </summary>
        public Int32 NbCashHands
        {
            get { return _nbCashHands; }
            set
            {
                _nbCashHands = value;
                Notify("NbCashHands");
            }
        }

        private double _amountRaked;

        /// <summary>
        /// Rake generated in Euros
        /// </summary>
        public Double AmountRaked
        {
            get { return _amountRaked; }
            set 
            {
                _amountRaked = value;
                Notify("AmountRaked");
            }
        }

        private double _amountWon;

        /// <summary>
        /// Amount won/lost  in Euros
        /// </summary>
        public Double AmountWon
        {
            get { return _amountWon; }
            set { _amountWon = value;
            Notify("AmountWon"); 
            }
        }

        private DateTime _dateStart;

        /// <summary>
        /// The datetime start for theses stats
        /// </summary>
        public DateTime DateStart
        {
            get { return _dateStart; }
            set { _dateStart = value;
            Notify("DateStart");
            }
        }

        private DateTime _dateEnd;

        /// <summary>
        /// The datetime end for theses stats
        /// </summary>
        public DateTime DateEnd
        {
            get { return _dateEnd; }
            set { _dateEnd = value;
            Notify("DateEnd");
            }
        }

        private double _fidelityPointEarned;

        /// <summary>
        /// FidelityPoint earned, depending ont he poker site and amount raked
        /// </summary>
        public Double FidelityPointEarned
        {
            get { return _fidelityPointEarned; }
            set { _fidelityPointEarned = value;
            Notify("FidelityPointEarned");
            }
        }

        private double _fppEarned;

        /// <summary>
        /// FPP Earned (7.5 VPP*VIP Status Multiplicator by Raked Euro)
        /// </summary>
        public Double FppEarned
        {
            get { return _fppEarned; }
            set { _fppEarned = value;
            Notify("FppEarned");
            }
        }

        /// <summary>
        /// Return the current Date time juste for testing purposes
        /// </summary>
        public DateTime LastUpdatedTime
        {
            get { return DateTime.Now; }
        }


        // Create the //OnPropertyChanged method to raise the event
        protected void Notify(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
               // Debug.WriteLine(string.Format("Property <{0}> changed fired ", name));

                handler(this, new PropertyChangedEventArgs(name));
            }
        }


        private Collection<PlayerStatsDetail> _sessionsDetailledStats= new Collection<PlayerStatsDetail>();

        /// <summary>
        /// The Detailled statistic for each session found in the queried period
        /// </summary>
        public Collection<PlayerStatsDetail> SessionsDetailledStats
        {
            get { return _sessionsDetailledStats; }
            set
            {
                _sessionsDetailledStats = value;
                Notify("SessionsDetailledStats");
                Notify("SessionsFormatedVPIP");
            }
            
        }


        /// <summary>
        /// Return the Formated VPIP % string, with 1 line per game limit
        /// </summary>
        public string SessionsFormatedVPIP
        {

            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (PlayerStatsDetail item in this.SessionsDetailledStats)
                {
                    if(SessionsDetailledStats.Count>0)
                        sb.AppendLine(string.Format("{0} (NL{1})", item.VPipPercent.ToString("#0.0"), item.BigBlindValue * 100));
                    else
                        sb.AppendLine(string.Format("{0}", item.VPipPercent.ToString("#0.0")));
                }
                if (sb.Length > 0)
                    return sb.ToString().Remove(sb.ToString().Length - 2, 2); //removing the last \r\n
                else
                    return "";

            }

        }

        /// <summary>
        /// Return the Formated WinRate % string, with 1 line per game limit
        /// </summary>
        public string SessionsFormatedWinRate
        {

            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (PlayerStatsDetail item in this.SessionsDetailledStats)
                {
                    if (SessionsDetailledStats.Count > 0)
                        sb.AppendLine(string.Format("{0} (NL{1})", item.WinRate.ToString("#0.0"), item.BigBlindValue * 100));
                    else
                        sb.AppendLine(string.Format("{0}", item.WinRate.ToString("#0.0")));
                }
                if (sb.Length > 0)
                    return sb.ToString().Remove(sb.ToString().Length - 2, 2); //removing the last \r\n
                else
                    return "";

            }

        }

    }
}
